//
//  LHPhotoShowController.m
//  WXYView
//
//  Created by lehuo on 2017/1/11.
//  Copyright © 2017年 lehuo. All rights reserved.
//

#import "LHPhotoShowController.h"
#import "PhotoGroupModel.h"
#import "LHPhotoCollectionCell.h"
#import "LHBrowseController.h"
#import "PhotoAssetModel.h"
#import "GoalData.h"
#import "LHAlertView.h"
#define CloumAndLineSpace 5

static NSString *PCCell = @"cell";
@interface LHPhotoShowController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *sendBtn;
@end

@implementation LHPhotoShowController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadView) name:@"ReloadView" object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pop) name:PostData object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



- (void)initView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat cellWH = ([UIScreen mainScreen].bounds.size.width - 5 * CloumAndLineSpace) / 4;
    layout.itemSize = CGSizeMake(cellWH, cellWH);
    layout.minimumLineSpacing = CloumAndLineSpace;
    layout.minimumInteritemSpacing = CloumAndLineSpace;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(CloumAndLineSpace, 0, kSceenW - 2 * CloumAndLineSpace, self.view.bounds.size.height) collectionViewLayout:layout];
    _collectionView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LHPhotoCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:PCCell];
     [self.view addSubview:_collectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithTitle:@"预览" style:UIBarButtonItemStylePlain target:self action:@selector(previewClick)];
    self.navigationItem.rightBarButtonItem = barBtn;
    
}

#pragma mark - UICollectionView代理

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PCCell forIndexPath:indexPath];
    [cell setCellValue:self.assets[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHBrowseController *browseVC = [[LHBrowseController alloc] init];
    browseVC.assets = self.assets;
    browseVC.indexPath = indexPath;
    browseVC.photoModels = self.photoModels;
    [self presentViewController:browseVC animated:YES completion:nil];
}

#pragma mark - 接到通知，销毁控制器
- (void)pop
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - 接到通知，刷新UI
- (void)reloadView
{
    [self.collectionView reloadData];
}

#pragma mark - 预览
- (void)previewClick
{
    NSUInteger index = - 1;
    for (PhotoAssetModel *assetModel in self.assets) {
        index ++;
        if(assetModel.isSelect)
        {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            LHBrowseController *browseVC = [[LHBrowseController alloc] init];
            browseVC.assets = self.assets;
            browseVC.indexPath = indexPath;
            browseVC.photoModels = self.photoModels;
            [self presentViewController:browseVC animated:YES completion:nil];
            return;
        }
    }
    [LHAlertView showSimpleInfo:@"你还没选择相片！"];
    
}


@end
